package tingshu.service;

import tingshu.data_object.TidToCountDO;
import tingshu.data_object.TrackDO;
import tingshu.repository.RelationRepo;
import tingshu.repository.TrackRepo;
import tingshu.util.Log;
import tingshu.view_object.PaginationView;
import tingshu.view_object.StudioTrackListView;
import tingshu.view_object.TrackListView;
import tingshu.view_object.UserVO;

import java.lang.management.ThreadInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author houdaiqin
 * @date 2022/07/14 10:23
 **/
public class TrackService {
    public final TrackRepo trackRepo=new TrackRepo();
    public final RelationRepo relationRepo=new RelationRepo();
    public StudioTrackListView listOfUser(UserVO userVO,int page){
        StudioTrackListView resultView=new StudioTrackListView();
        resultView.currentUser=userVO;

        //规定每页五条
        int countPerPage=5;

        //总的条数
        int count= trackRepo.selectCountByUid(userVO.uid);
        //总共的页数
        int totalPage=count/countPerPage;
        if(count%countPerPage>0){
            totalPage++;
        }

        if (page<1){
            Log.println("页面信息小于最小值，调整为 1");
            page = 1;
        }
        if (page > totalPage) {
            Log.println("页面信息大于最大值，调整为最大值");
            page = totalPage;
        }
        int offset=(page-1)*countPerPage;
        //1.从数据库表中查询获取指定页的歌曲信息
        List<TrackDO> list=trackRepo.selectListByUidLimitOffset(userVO.uid,countPerPage,offset);
        Log.println("从数据库中获取的数据"+list);

        if(!list.isEmpty()) {
            Log.println("结果不为空，所以需要继续查询关联数量");
            // 2. 根据结果，查询每个 track 对应的 refCount（引用次数）
            // 2.1 先从 list 得到每一个 tid
            List<Integer> tidList=new ArrayList<>();
            for(TrackDO trackDO:list){
                tidList.add(trackDO.tid);
            }
            // 2.2 拿着 tidList 从 relations 表中得到需要的结果 Map<tid, refCount>
            Map<Integer, Integer> tidToRefCountMap=new HashMap<>();
            //将tidList传入relationRepo的方法中，查找出每首音频关联的专辑数，将音频id即tid和关联专辑的数量refCount存入map中
            List<TidToCountDO> tidToCountDOList=relationRepo.selectCountByTidListGroupByTidList(tidList);
            Log.println("查询relations表中的结果"+tidToCountDOList);
            for(TidToCountDO tidToCountDO:tidToCountDOList){
                tidToRefCountMap.put(tidToCountDO.tid,tidToCountDO.refCount);
            }
            Log.println("打印Map"+tidToRefCountMap);

            //2.将TrackDo --> ListTrackView
            List<TrackListView> resultList = new ArrayList<>();
            for (TrackDO trackDO : list) {
                TrackListView view = new TrackListView(trackDO,tidToRefCountMap.getOrDefault(trackDO.tid,0));
                resultList.add(view);
            }
            Log.println("转换成要输出的结果" + resultList);
            resultView.trackList = resultList;
        }else{
            Log.println("list为空，所以关联数量就不管了");
            resultView.trackList = new ArrayList<>();
        }

        //获取分页信息
        PaginationView paginationView=new PaginationView();
        paginationView.countPerPage=countPerPage;
        paginationView.currentPage=page;
        paginationView.totalPage=totalPage;
        resultView.pagination=paginationView;

        return resultView;
    }
}